#include "graphTask.h"
#include "bitmaphex.h"
#include "I2CTaskMsgTypes.h"
#include "vtI2C.h"
#include "i2cTemp.h"
#include "UartTask.h"
// Helper Functions Declaration
#define PI 3.14159265
//define this to enter the debug mode
#define __GraphDebug  1
static void Draw_Grids();
static void Graph(unsigned int X , unsigned int Y);
static int GetVoltage();
static void vTimerCallback( xTimerHandle pxTimer );
xTimerHandle xTimer;
xQueueHandle GraphQueue;

unsigned int x_=0;
unsigned short tscr;
portTickType xLastWakeTime;
const portTickType xFrequency = 10 / portTICK_RATE_MS;
const portTickType x_Delay = 2000 / portTICK_RATE_MS;
//

void GraphTaskCode( void * pvParameters )
{
	unsigned int test = 0;
	unsigned int testy = 0;
	
	/* Initialize the LCD and set the initial colors */
	GLCD_Init();
	tscr = Green; // may be reset in the LCDMsgTypeTimer code below
	GLCD_Clear(Red);
	GLCD_SetTextColor(tscr);
	GLCD_SetBackColor(Red);
	GLCD_Clear(Red);



	//Draw_Grids();
	long x=1;
	struct UARTPacket * voltage;
    struct UARTPacket Msg;
	int delayCounter =0;
	int counter = 0;

  for( ;; )
  {

   #if __GraphDebug  ==0
	testy = (unsigned int)(sin(counter/20.0)*30 + 190);
	unsigned char* hex = Green;
	GLCD_Bmp(x_,testy,1,1,hex);
	counter++;
	x_++;
	xLastWakeTime = xTaskGetTickCount();
	vTaskDelayUntil( &xLastWakeTime, xFrequency );
	if(x_==320){
	GLCD_Clear(Red);
	Draw_Grids();
	 x_=0;
	}
	#endif

	#if __GraphDebug  ==1	
      /* Task code goes here. */
	  					
	 if( GraphQueue != 0 )
    {
        // Receive a message on the created queue.  Block for 10 ticks if a
        // message is not immediately available.
        if( xQueueReceive( GraphQueue, &( voltage ), ( portTickType ) 10 ) )
        {
			
           
			struct UARTPacket v;
			//v = voltage->value;
//			unsigned char X_label[2];
//			v.value  = 2;//voltage->value;
//			snprintf(X_label, 2, "%d",v.value);
			
//	 		
			memcpy(&v,voltage,sizeof(v));
			//v = &voltage;
			uint8_t value = v.value;//voltage->value;
			//val = (int) voltage->value;
			//v.value = 0x55;
			//v = *voltage;
			//memcpy (&v,voltage,sizeof(v));
			   //voltage->value = 0x55;
			unsigned char buff[10];
			print_hex_packet(&value,sizeof(value),buff);
			GLCD_DisplayString(x_,10,0.3,(unsigned char*)buff);
			

			
			//snprintf(X_label, sizeof(X_label), "%d",val);
//			unsigned char* X_label = "z";
	 		//GLCD_DisplayString(x_,39,0.3,X_label);
			//unsigned int y_axis = (unsigned int) v.value;
			//float scale = 160/1023;
			//y_axis= (y_axis*16/65280);
			//y_axis-=80;
			//y_axis/=20;
			//unsigned int y_init=0;
			//unsigned char* hex = Green;
//			if(y_axis>1000)
//			y_axis = 160;
//			else
//			y_axis = 10;
//			if(y_init!=y_axis){
//			  int j;
//			  for(j=10;j<160;j++){
//			   //GLCD_Bmp(x_,j,1,1,hex);
//			  }
//			}
			//unsigned char* hex = Green;
			//y_axis = (unsigned int)(sin(y_axis/10.0)*30 + 190);
			//GLCD_Bmp(x_,y_axis+100,1,1,hex);
			x_++;
			//y_init = y_axis;
			if(x_==30){
			GLCD_Clear(Red);
			//Draw_Grids();
			x_=0;
			}	
			
        }
    }
	
#endif
		}
		// We will never get here
	
}


void InitGraphTask(){
 static unsigned char ucParameterToPass;
	xTaskHandle GraphHandle;

	
  /* Create the task, storing the handle.  Note that the passed parameter 
  ucParameterToPass must exist for the lifetime of the task, so in this 
  case is declared static.  If it was just an an automatic stack variable 
  it might no longer exist, or at least have been corrupted, by the time
  the new task attempts to access it. */
  xTaskCreate( GraphTaskCode, ( signed char * ) "Graph Task", 20*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY+1
  , &GraphHandle );
  GraphQueue = xQueueCreate( 100, sizeof(struct UARTPacket)  );

}


static void Draw_Grids(){
	unsigned int X_axes = 0;
	unsigned int Y_axes = 220;
	int grids = 0;
	// draw the X axis

	
	for (grids; grids<320;grids++){
		Graph(X_axes,Y_axes);
		X_axes++;
		if(grids%64==0){
		int i=0;
		for (i;i<5;i++){
		Graph(X_axes-10, Y_axes-i);
		}

		}
	}
	
	// draw the Y axis
	grids = 0;
	X_axes = 10;
	Y_axes = 0;
	
	for (grids; grids<240;grids++){
	Graph(X_axes,Y_axes);
	Y_axes++;
	if(grids%40==0){
		int i=0;
		for (i;i<5;i++){
		Graph(X_axes+i, Y_axes-20);
		if(Y_axes<20){break;}
		}

		}
	}
	
	 unsigned char* X_label = "Time 0.5ms/Div";
	 GLCD_DisplayString(28,39,0.3,X_label);
	 X_label = "Voltage 1V/Div";
	 GLCD_DisplayString(1,5,0.3,X_label);
	 
}

static void Graph(unsigned int X , unsigned int Y){
	
	//unsigned int amplitude = Y;
	taskENTER_CRITICAL();
	
	GLCD_PutPixel(X, Y);

	taskEXIT_CRITICAL();
}




